package t2h.activities;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.sun.lwuit.list.DefaultListModel;
import com.sun.lwuit.list.ListModel;

import t2h.valueobjects.SongVO;

public class ReadXmlFileWithoutLyric extends DefaultHandler {

       private SongVO acct;
       private String temp;
       //private ArrayList accList = new ArrayList<SongVO>();
       ListModel accList = new DefaultListModel();

       //** The main method sets things up for parsing *//*
       public ListModel getListKara() throws IOException, SAXException,
                     ParserConfigurationException {
             
              //Create a "parser factory" for creating SAX parsers
              SAXParserFactory spfac = SAXParserFactory.newInstance();

              //Now use the parser factory to create a SAXParser object
              SAXParser sp = spfac.newSAXParser();

              //Create an instance of this class; it defines all the handler methods
              ReadXmlFile handler = new ReadXmlFile();

              //Finally, tell the parser to parse the input and notify the handler
              //String fileName = "/Vol471.xml";
              String fileName = "/Vol47.xml";
              //File fXMLFile = new File(fileName);
              InputStream inputStream= this.getClass().getResourceAsStream(fileName);
              Reader reader = new InputStreamReader(inputStream,"UTF-8");
          	 
              InputSource is = new InputSource(reader);
              is.setEncoding("UTF-8");
              sp.parse(is,handler);
             
              return handler.getaccListKara();

       }

        
       public void characters(char[] buffer, int start, int length) {
              temp = new String(buffer, start, length);
       }
      

         
       public void startElement(String uri, String localName,
                     String qName, Attributes attributes) throws SAXException {
              temp = "";
              if (qName.equalsIgnoreCase("song")) {
                     acct = new SongVO();
                     acct.setKaraokeType(attributes.getValue("type"));

              }
       }
       
       public ListModel getaccListKara(){
    	   return accList;
       }

       
        //* When the parser encounters the end of an element, it calls this method
        
       public void endElement(String uri, String localName, String qName)
                     throws SAXException {

              if (qName.equalsIgnoreCase("song")) {
                     // add it to the list
                     accList.addItem(acct);

              } else if (qName.equalsIgnoreCase("name")) {
                     acct.setName(temp);
              } else if (qName.equalsIgnoreCase("id")) {
                     acct.setId(temp);
              } else if (qName.equalsIgnoreCase("author")) {
                     acct.setAuthor(temp);
              }else if (qName.equalsIgnoreCase("lyric")) {
                  acct.setLyric(temp);
	          }
       }
}